{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 三大特性-多态"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "class Dog(object):\r\n",
    "    def work(self):  # 父类提供统一的方法，哪怕是空方法\r\n",
    "        print('指哪打哪...')\r\n",
    "\r\n",
    "\r\n",
    "class ArmyDog(Dog):  # 继承Dog类\r\n",
    "    def work(self):  # 子类重写父类同名方法\r\n",
    "        print('追击敌人...')\r\n",
    "\r\n",
    "\r\n",
    "class DrugDog(Dog):\r\n",
    "    def work(self):\r\n",
    "        print('追查毒品...')"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "class Person(object):\r\n",
    "    def work_with_dog(self, dog):  # 传入不同的对象，执行不同的代码，即不同的work函数\r\n",
    "        dog.work()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "ad = ArmyDog()\r\n",
    "dd = DrugDog()\r\n",
    "\r\n",
    "daqiu = Person()\r\n",
    "daqiu.work_with_dog(ad)\r\n",
    "daqiu.work_with_dog(dd)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "追击敌人...\n",
      "追查毒品...\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 类属性和类方法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "# 设置和访问类属性\r\n",
    "class Dog(object):\r\n",
    "    tooth = 10\r\n",
    "\r\n",
    "\r\n",
    "wangcai = Dog()\r\n",
    "xiaohei = Dog()\r\n",
    "\r\n",
    "print(Dog.tooth)  # 10\r\n",
    "print(wangcai.tooth)  # 10\r\n",
    "print(xiaohei.tooth)  # 10"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "10\n",
      "10\n",
      "10\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "# 修改类属性\r\n",
    "class Dog(object):\r\n",
    "    tooth = 10\r\n",
    "\r\n",
    "\r\n",
    "wangcai = Dog()\r\n",
    "xiaohei = Dog()\r\n",
    "\r\n",
    "# 修改类属性\r\n",
    "Dog.tooth = 12\r\n",
    "print(Dog.tooth)  # 12\r\n",
    "print(wangcai.tooth)  # 12\r\n",
    "print(xiaohei.tooth)  # 12"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "12\n",
      "12\n",
      "12\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "# 不能通过对象修改属性，如果这样操作，实则是创建了一个实例属性\r\n",
    "wangcai.tooth = 20\r\n",
    "print(Dog.tooth)  # 12\r\n",
    "print(wangcai.tooth)  # 20\r\n",
    "print(xiaohei.tooth)  # 12"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "12\n",
      "20\n",
      "12\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "# 实例属性\r\n",
    "class Dog(object):\r\n",
    "    def __init__(self):\r\n",
    "        self.age = 5\r\n",
    "\r\n",
    "    def info_print(self):\r\n",
    "        print(self.age)\r\n",
    "\r\n",
    "\r\n",
    "wangcai = Dog()\r\n",
    "print(wangcai.age)  # 5\r\n",
    "# print(Dog.age)  # 报错：实例属性不能通过类访问\r\n",
    "wangcai.info_print()  # 5"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "5\n",
      "5\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 类方法和静态方法"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "# 类方法\r\n",
    "class Dog(object):\r\n",
    "    __tooth = 10\r\n",
    "\r\n",
    "    @classmethod\r\n",
    "    def get_tooth(cls):\r\n",
    "        return cls.__tooth\r\n",
    "\r\n",
    "\r\n",
    "wangcai = Dog()\r\n",
    "result = wangcai.get_tooth()\r\n",
    "print(result)  # 10"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "10\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "# 静态方法\r\n",
    "class Dog(object):\r\n",
    "    @staticmethod\r\n",
    "    def info_print():\r\n",
    "        print('这是一个狗类，用于创建狗实例....')\r\n",
    "\r\n",
    "\r\n",
    "wangcai = Dog()\r\n",
    "# 静态方法既可以使用对象访问又可以使用类访问\r\n",
    "wangcai.info_print()\r\n",
    "Dog.info_print()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "这是一个狗类，用于创建狗实例....\n",
      "这是一个狗类，用于创建狗实例....\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.6.4",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}